//
//  PopTransitionAnimation.m
//  29-HomeWork_TransitionAnimation
//
//  Created by dllo on 16/8/23.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "PopTransitionAnimation.h"
#import "ViewController.h"
#import "SecondViewController.h"
#import "MyTableViewCell.h"
@implementation PopTransitionAnimation


- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    SecondViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    ViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    // 取出cell
    MyTableViewCell *cell = [toVC.tableView cellForRowAtIndexPath:toVC.currentIndexPath];
    [containerView addSubview:toVC.view];
    cell.imageOfCell.hidden = YES;
    //截图
    UIView *snopView = [fromVC.toImageView snapshotViewAfterScreenUpdates:NO];
    snopView.frame = fromVC.toImageView.frame;
    [containerView addSubview:snopView];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        CGRect newRect_cell = [toVC.view convertRect:cell.contentView.frame fromView:cell];
        snopView.frame = newRect_cell;
    } completion:^(BOOL finished) {
        cell.imageOfCell.hidden = NO;
        [snopView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
   
    
    
    
}

@end
